package com.lz.sports.core

import android.media.SoundPool
import com.lz.comm.SP
import com.lz.library.extension.APP

//声音播报
class SoundHelper {

    //声音播报
    private val soundPool: SoundPool = SoundPool.Builder().setMaxStreams(3).build()

    //已经初始化
    private var hasInit = false

    //初始化后的id
    private lateinit var soundIds: IntArray

    //音量
    private val volume: Float = SP.getInt(K_SOUND_VOLUME, v_sound_volume) / 100f

    fun init(resIds: IntArray) {
        if (!hasInit) {
            hasInit = true
            soundIds = IntArray(resIds.size)
            for (i in soundIds.indices) {
                soundIds[i] = soundPool.load(APP, resIds[i], 1)
            }
        }
    }

    fun release() {
        soundPool.release()
        hasInit = false
    }

    fun play(index: Int) {
        play(index, volume)
    }

    //播放索引与音量
    fun play(index: Int, volume: Float) {
        if (hasInit && index in soundIds.indices) {
            soundPool.play(soundIds[index], volume, volume, 1, 0, 1.0f)
        }
    }
}